//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Cloud.Push
{
    using System;
    using Microsoft.WindowsAzure;
    using Model;

    public class CloudPushOptions
    {
        private CloudPushOptions()
        {
        }

        public PreflightContext PreflightContext
        {
            get;
            private set;
        }

        public bool Repeat
        {
            get;
            private set;
        }

        public TimeSpan RepeatInterval
        {
            get;
            private set;
        }

        public CloudStorageAccount CloudStorageAccount
        {
            get;
            private set;
        }

        public static CloudPushOptions OneTimePush(PreflightContext context, CloudStorageAccount account)
        {
            return CreateOptions(context, account, false, TimeSpan.Zero);
        }

        public static CloudPushOptions OneTimePush(IVerificationFactory factory, CloudStorageAccount account)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

            var context = new PreflightContext(factory, new PreflightOptions());
            return OneTimePush(context, account);
        }

        public static CloudPushOptions RepeatPush(PreflightContext context, CloudStorageAccount account, TimeSpan interval)
        {
            return CreateOptions(context, account, true, interval);
        }

        public static CloudPushOptions RepeatPush(IVerificationFactory factory, CloudStorageAccount account, TimeSpan interval)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

            var context = new PreflightContext(factory, new PreflightOptions());
            return RepeatPush(context, account, interval);
        }

        public static CloudPushOptions CreateOptions(PreflightContext context, CloudStorageAccount account, bool repeat, TimeSpan repeatInterval)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (account == null)
            {
                throw new ArgumentNullException("account");
            }

            if (repeat && repeatInterval <= TimeSpan.Zero)
            {
                throw new ArgumentException("Repeat interval must be greater than 0", "repeatInterval");
            }

            return new CloudPushOptions
                {
                    PreflightContext = context,
                    CloudStorageAccount = account,
                    Repeat = repeat,
                    RepeatInterval = repeatInterval
                };
        }
    }
}
